package org.nokatag.tagjava;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.nokatag.system.BugInfoWrint;
import org.nokatag.system.Concest;
import org.nokatag.system.NLoadFun;

/**
 * 按钮标签
 * @author rebin
 * 
 */
public class Button extends Inputsuper{
	private static final long serialVersionUID = 2059077049501772497L;
	private String formurl = null;//提交form的url
	private String formid = null;//提交form的ID
	private String val = null;//设置成变量
	private String shortcutkey=null;//快捷键  altKey|shiftKey|ctrlKey+ctrlKey+13
	
	public int doStartTag()throws JspException{
		JspWriter jspOut = pageContext.getOut();
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		String sformid = "";
		boolean isShow = true;//是否显示
		//----------获取表单id-----------------------------------------
		if(formid!=null){
			sformid = formid;
		}else{
			FormHtml fh = getTagFormHtml(this);
			if(null!=fh){
				sformid = fh.getId();
			}
		}
		//--------------判断是否显示------------------------------------
		if(Concest.BUTTON_FILTER_CLASS!=null){
			try{
				NLoadFun nloadFun = new NLoadFun();
				ButtonInterface dain = (ButtonInterface)nloadFun.getBean(Concest.BUTTON_FILTER_CLASS);
				isShow=dain.show(pageContext,this);
			}catch(Exception se){
				se.printStackTrace();
			}
		}
		//---------------生成按钮--------------------------------------
		if(isShow){
			FormHtmlInsertid(getId(),getName(),getValue());
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.append(potjs(request));
			stringBuilder.append(nkUtiljs(request));
			stringBuilder.append("<input type=\"button\" ");
			if(formurl!=null){
				stringBuilder.append(" onclick=\"nk_form_button_ajaxSubmit('"+sformid+"','"+formurl+"')\" ");
				NOOUT.put("onclick", true);
			}
			stringBuilder.append(getBaseStr()+"/>");
			stringBuilder.append("<script type=\"text/javascript\">");
			stringBuilder.append("Event.observe(document, 'dom:loaded', function() {");
			//----------------------------------
			stringBuilder.append("$('"+getId()+"').setDisabled = function(a){");
			stringBuilder.append("	$('"+getId()+"').writeAttribute({disabled:a});");
			stringBuilder.append("};");
			stringBuilder.append("$('"+getId()+"').setReadonly = function(a){");
			stringBuilder.append("	$('"+getId()+"').writeAttribute({readonly:a});");
			stringBuilder.append("};");
			stringBuilder.append("});");
			stringBuilder.append("</script>");
			stringBuilder.append(shortcutkey(shortcutkey,getId(),null));
			try {
				if(val!=null)
					request.setAttribute(val, stringBuilder.toString());
				else
					jspOut.write(stringBuilder.toString());
			} catch (IOException e) {
				BugInfoWrint.Print(e);
			}
		}
		return EVAL_BODY_INCLUDE;
	}

	public String getFormurl() {
		return formurl;
	}

	public void setFormurl(String formurl) {
		this.formurl = formurl;
	}

	public String getVal() {
		return val;
	}

	public void setVal(String val) {
		this.val = val;
	}

	public String getFormid() {
		return formid;
	}

	public void setFormid(String formid) {
		this.formid = formid;
	}

	public String getShortcutkey() {
		return shortcutkey;
	}

	public void setShortcutkey(String shortcutkey) {
		this.shortcutkey = shortcutkey;
	}
}
